![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Use Explicit PropertiesControls have default properties. A program can refer to a controls default property by referring to the control. For example, the following two lines of code are equivalent. NumEmployees = CInt(txtNumEmployees.Text) NumEmployees = CInt(txtNumEmployees) Accessing a controls default property without explicitly mentioning it is not obvious. It forces the reader to remember the controls type and to remember the default property for that kind of control. Make it obvious which property is being used by explicitly referencing the properties of controls, forms, and other objects. Eliminate Random BehaviorRandom behavior makes bugs hard to find. If you cannot repeat the series of steps that lead to incorrect behavior, you may have a hard time reproducing a bug. You can make things more repeatable by removing random behavior wherever possible. You cannot remove all randomness from your system because you do not have complete control over the other applications that are running. Still, you can limit the random behavior of your own application by removing randomness wherever you find it. Certain operations can appear random to a program. Obviously the Rnd statement introduces randomness. Examining the system time, file system, or network connections may also cause unrepeatable behavior. If a subroutines operation depends on the number of files in a certain directory, it may behave differently when there are different numbers of files. For example, the routine may crash if no files are present. If that occurs very infrequently, it may be hard for you to reproduce the problem. Pay special attention to code that uses values like system characteristics that are outside the control of the program. While you may be unable to remove these dependencies, you can at least record the values the program uses. Then if an error occurs, you can examine these values to see if they may be related to the problem. For example, many programs use random numbers generated by the Rnd statement. If you remove this randomness, you may destroy the program. While you cannot eliminate these random numbers, you can control them. The SeedRnd subroutine shown in the following code initializes the random number generator. It takes a parameter indicating whether it should generate a new random sequence or reuse the previous one. To generate a new sequence, the routine passes the Randomize statement the number of seconds since midnight. It then saves this seed value in the system registry in case it needs the value again the next time the program runs. To repeat the previous sequence, the routine uses GetSetting to retrieve the seed value saved in the registry. It passes that value to the Randomize statement so the Rnd function will use the previous sequence of random numbers.
Seed the random number generator. If use_previous
is true, seed the generator using the previous
value stored in the system registry. Otherwise
use the number of second since midnight and save
that number in the registry.
Public Sub SeedRnd(ByVal use_previous As Boolean)
Dim seed As Long
If use_previous Then
Get the seed from the registry. Use the
seconds past midnight as a default.
seed = GetSetting(MyProgram, _
Configuration, RndSeed, Timer)
Else
Get the seconds after midnight.
seed = Timer
End If
Initialize the random number generator.
Rnd -1
Randomize seed
Save the seed for next time.
SaveSetting MyProgram, Configuration, _
RndSeed, seed
End Sub
A program should call SeedRnd when it starts. It should not use the Randomize statement anywhere else. Normally the program calls SeedRand with parameter False to make it start a new sequence of random numbers. If you run across an obscure bug that is hard to reproduce and you think it may be caused by a certain sequence of random numbers, you should pass SeedRnd the value True to make it reuse the same random sequence it used before. If the bug now becomes easily repeatable, you should suspect that it has something to do with the programs use of the Rnd statement. If the bug remains hard to reproduce, you should suspect behavior outside of the programs control. Perform Short Actions FirstSuppose you are reading a subroutine and you come to an Else statement. If it has been a long time since you read the corresponding If clause, you may not remember what condition it tests. This can be even more confusing with a series of nested If statements.
If condition1 Then
:
Many lines of code.
:
If condition2 Then
More code.
:
Else
More code.
:
End If
Else Which condition is this?
Do something brief.
End If
You can make the code a bit more obvious if you place short actions first. When the reader reaches the Else clause, the If statement may still be visible on the screen. In that case, it is easy to decipher the meaning of the Else statement.
If Not condition1 Then
Do something brief.
Else It is obvious which condition this is.
:
Many lines of code.
:
If condition2 Then
More code.
:
Else
More code.
:
End If
End If
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|